<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/8/15
 * Time: 9:16
 */

namespace User\Controller;


use Common\Controller\AdminbaseController;

class PromotionController extends AdminbaseController
{

    //  活动列表
    public function index(){

        $list = D("Promotion")->select();
        foreach($list as $k => $v){
            $count = D("PromotionProduct")->where(array('promotion_id'=>$v['id']))->count();
            $list[$k]['count'] = $count;
        }
        $this->assign('list',$list);
        $this->display();
    }

    //  添加活动
    public function add(){

        if(IS_POST){
            if(!$_POST['start_time'] || !$_POST['end_time']){
                $this->error('请选择时间段');
            }
            $data = array(
                'create_time'=>time(),
            );
            $data['start_time'] = strtotime(I('post.start_time'));
            $data['end_time'] = strtotime(I('post.end_time'));
            $id = D("Promotion")->add($data);
            if($id){
                $ids = I('id',array());
                foreach($ids as $v){
                    $mid = array(
                        'product_id'=>$v,
                        'promotion_id'=>$id
                    );
                    D("PromotionProduct")->add($mid);
                }
                $this->success("添加成功",U('Promotion/index'));
                exit();
            }
        }
        $list = D("Product")
            ->where(array('status'=>array('gt',-1)))
            ->field('id,title,image,status,price_plus')
            ->select();
        $this->assign('list',$list);
        $this->display();
    }

    // 编辑活动
    public function edit(){
        if(IS_POST){
           $post = I('post.',array());
           $id = $post['pro_id'];
           if(!$post['start_time'] || !$post['end_time']){
            $this->error('请选择时间段');
           }
           $save = array(
               'start_time' => strtotime($post['start_time']),
               'end_time' => strtotime($post['end_time'])
           );
//        dump($save);die;
            D("Promotion")->where(array('id'=>$id))->save($save);

            D("PromotionProduct")->where(array('promotion_id'=>$id))->delete();
            $ids = I('id',array());
            foreach($ids as $v){
                $mid = array(
                    'product_id'=>$v,
                    'promotion_id'=>$id
                );
                D("PromotionProduct")->add($mid);
            }
            $this->success('编辑成功',U('Promotion/index'));
            exit();

        }
        $id = I('get.id',0);
        $data = D("Promotion")->where(array('id'=>$id))->find();
        $this->assign('data',$data);
        $list = D("Product")
            ->where(array('status'=>array('gt',-1)))
            ->field('id,title,image,status,price_plus')
            ->select();
        $this->assign('list',$list);
        $in_list = D("PromotionProduct")->where(array('promotion_id'=>$id))->select();
        $in_promotion = array();
        foreach($in_list as $v){
           $in_promotion[] = $v['product_id'];
        }
        $this->assign('in_promotion',$in_promotion);
        $this->display();
    }

    // 删除
    public function delete(){
        $id = I('get.id',0);
        D("PromotionProduct")->where(array('promotion_id'=>$id))->delete();
        $rm = D("Promotion")->where(array('id'=>$id))->delete();
        if($rm){
            $this->success('删除成功',U('Promotion/index'));
            exit();
        }
    }
}